﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PKURG.CostSplit.Business;
using System.Data;

public partial class Pages_StageArea : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitTitle();
        }
        if (gvContractList.Columns.Count >= 1)
        {
            gvContractList.Columns[gvContractList.Columns.Count - 1].Visible = CurrentUserInfo.RwType != 0;

        }
    }

    private void InitTitle()
    {
        string stageIdString = Request["id"];
        if (string.IsNullOrEmpty(stageIdString))
        {
            Common.GoErrorPage();
            return;
        }
        var info = Dev_Stage.GetInfo(stageIdString);
        if (info == null)
        {
            Common.GoErrorPage();
            return;
        }
        lbTitle.Text = string.Format("{0} {1}", info.ProjectName, info.StageName);

    }

    protected void gvContractList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = e.Keys[0].ToString();

        string tbBuildingCount = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbBuildingCount")).Text;
        string tbZD_Area = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbZD_Area")).Text;
        string tbJZ_Area = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbJZ_Area")).Text;
        string tbJZ_U_Area = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbJZ_U_Area")).Text;
        string tbJZ_D_Area = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbJZ_D_Area")).Text;
        string tbXS_Area = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbXS_Area")).Text;
        string tbXS_U_Area = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbXS_U_Area")).Text;
        string tbXS_D_Area = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbXS_D_Area")).Text;

        //平安夜前新增四个面积
        string tbMixArea = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbMixArea")).Text;
        string tbMixAreaD2 = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbMixAreaD2")).Text;
        string tbMixAreaD3 = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbMixAreaD3")).Text;
        string tbMixAreaD6 = ((TextBox)gvContractList.Rows[e.RowIndex].FindControl("tbMixAreaD6")).Text;


        string stageId = Request["id"];

        BusinessTypeArea.UpdateInfo(stageId, id, tbBuildingCount.ToDecString(), tbZD_Area.ToDecString(), tbJZ_Area.ToDecString(), tbJZ_U_Area.ToDecString(), tbJZ_D_Area.ToDecString(), tbXS_Area.ToDecString(),
tbXS_U_Area.ToDecString(), tbXS_D_Area.ToDecString(),tbMixArea,tbMixAreaD2,tbMixAreaD3,tbMixAreaD6);


        e.Cancel = true;
        gvContractList.EditIndex = -1;
        gvContractList.DataBind();
    }

    private string Filter(string tbBuildingCount)
    {
        throw new NotImplementedException();
    }

    protected void gvContractList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // 合计
        //if (e.Row.RowType == DataControlRowType.Footer)
        //{
        //    e.Row.Cells.Clear();
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = "合计",
        //        HorizontalAlign = HorizontalAlign.Center
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x =>Convert.ToInt32(x.BuildingCount)).ToString()
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x => (Decimal)(Convert.ToDecimal(x.ZD_Area))).ToString()
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x => (Decimal)(Convert.ToDecimal(x.JZ_Area))).ToString()
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x => (Decimal)(Convert.ToDecimal(x.JZ_U_Area))).ToString()
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x => (Decimal)(Convert.ToDecimal(x.JZ_D_Area))).ToString()
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x => (Decimal)(Convert.ToDecimal(x.XS_Area))).ToString()
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x => (Decimal)(Convert.ToDecimal(x.XS_U_Area))).ToString()
        //    });
        //    e.Row.Cells.Add(new TableCell()
        //    {
        //        Text = infos.Sum(x => (Decimal)(Convert.ToDecimal(x.XS_D_Area))).ToString()
        //    });
        //}
    }

    List<dynamic> infos;
    protected void odsData_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue==null)
        {
            return;
        }

        infos=e.ReturnValue as List<dynamic>;
    }
    protected void btnSyncData_Click(object sender, EventArgs e)
    {
        string stageId = Request["id"];
        int ret=BusinessTypeArea.SyncStageArea(stageId);
        gvContractList.DataBind();
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "2", "alert('同步完成')", true);

    }
}